if exists (select 1 from sysobjects where name = 'chk_dienstplan' and type = 'P')
begin
   drop procedure chk_dienstplan
   print 'Procedure: chk_dienstplan deleted ...'
end
go
create procedure chk_dienstplan(
   @prsID          int  = 1,
   @datum          char(8) = '20070501',
   @planID         int  = 1
)
as
begin
  set nocount on
   declare @exists int
   declare @dienstid int

   select @exists = 0
   
   if exists (select 1 from Dienstplan where PrsID = @prsID and Datum = @datum and PlanID not in (@planID))
    begin
       select @exists = 1 -- bereits belegt in einem anderen Plan
    end

--   if exists (select 1 from dienstplan where prsid = @prsid and datum = @datum and dienstid = 2)
--    begin
--       select @exists = 2 -- Ferien
--    end

   select 'exists' = @exists

end
go
print 'Procedure: chk_dienstplan done ...'
go
grant exec on chk_dienstplan to prsadmins with grant option
go
grant exec on chk_dienstplan to prsusers
go

